<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <link rel="stylesheet" href="styles/style.css">
        <link rel="stylesheet" href="styles/theme/atom-one-dark.css">
        <title>例子16-ES6/7常用语法介绍</title>
    </head>
    <body>
        <pre>
            <code>
                // 例子16

                // 设置默认参数
                function Point(x = 0, y = 0) {
                    this.x = x;
                    this.y = y;
                }

                const p = new Point(3);
                console.log(p); // Point {x:3, y:0}

                //--------------------------------------------------------------------------
                // 不可以在函数体内再对同名变量使用let或const声明
                function foo(x = 5) {
                    let x = 1; // Uncaught SyntaxError: Identifier 'x' has already been declared
                    const x = 2; // Uncaught SyntaxError: Identifier 'x' has already been declared
                }
                foo();

                //--------------------------------------------------------------------------

                // 设置参数默认值后，函数的length属性失真
                function fn1(x, y, z){};
                function fn2(x = 0, y, z){};
                function fn3(x, y = 0, z){};
                function fn4(x, y, z = 0){};
                function fn5(x, y, ...z){};

                console.log(fn1.length, fn2.length, fn3.length, fn4.length, fn5.length); // 3 0 1 2 2

                //--------------------------------------------------------------------------
                // 设置默认值时，形参初始化时形成独立作用域的影响
                let x = 1; // 如果没有这句的话，调用fn会报错：Uncaught ReferenceError: x is not defined
                function f(y = x) {
                    let x = 2;
                    console.log(y);
                }

                f(); // 1


                let foo = 'outer';
                function bar(func = () => foo) {
                    let foo = 'inner';
                    console.log(func());
                }

                bar(); // outer
            </code>
        </pre>
        <script src="scripts/libs/highlight.pack.js"></script>
        <script src="scripts/16.js"></script>
        <script>
            hljs.initHighlightingOnLoad();
        </script>
    </body>
</html>